import { toRaw } from "vue"
import config from '@/config';
import request from "./request";
import ossUpload from '@/js_sdk/jason-alioss-upload/oss';

export const gotoPage = (page ?: string) => {
	if (!page) return
	let tabBar = [
		'/pages/index/index',
		'/pages/chat-list/chat-list',
		'/pages/user/user',
	]
	if (tabBar.indexOf(page) >= 0) {
		uni.switchTab({
			url: page
		})
	} else {
		uni.navigateTo({
			url: page
		})
	}
}

export const customUrl = (item : any) => {
	let data = toRaw(item)
	if (data.ccshopPage) {
		// 小程序页面
		switch (data.ccshopPage.page_type) {
			case '0':
				gotoPage(data.ccshopPage.page_url)
				break;
			case '1':
				webview(data.ccshopPage.page_url)
				break;
		}
	}
}

export const webview = (url : string) => {
	uni.navigateTo({
		url: `/pages/webview/webview?url=${url}`
	})
}

export const tip = (opt : any, to_url ?: any) => {
	if (typeof opt == 'string') {
		to_url = opt;
		opt = {};
	}
	let title = opt.title || '',
		icon = opt.icon || 'none',
		endtime = opt.endtime || 2000,
		success = opt.success;
	if (typeof title != 'string') title = JSON.stringify(title)
	if (title) uni.showToast({
		title: title,
		icon: icon,
		duration: endtime,
		success
	})
	if (to_url != undefined) {
		if (typeof to_url == 'object') {
			let tab = to_url.tab || 1,
				url = to_url.url || '';
			switch (tab) {
				case 1:
					//一定时间后跳转至 table
					setTimeout(function () {
						uni.switchTab({
							url: url
						})
					}, endtime);
					break;
				case 2:
					//跳转至非table页面
					setTimeout(function () {
						uni.navigateTo({
							url: url,
						})
					}, endtime);
					break;
				case 3:
					//返回上页面
					setTimeout(function () {
						// #ifndef H5
						uni.navigateBack({
							delta: parseInt(url),
						})
						// #endif
						// #ifdef H5
						history.back();
						// #endif
					}, endtime);
					break;
				case 4:
					//关闭所有页面，打开到应用内的某个页面
					setTimeout(function () {
						uni.reLaunch({
							url: url,
						})
					}, endtime);
					break;
				case 5:
					//关闭当前页面，跳转到应用内的某个页面
					setTimeout(function () {
						uni.redirectTo({
							url: url,
						})
					}, endtime);
					break;
			}

		} else if (typeof to_url == 'function') {
			setTimeout(function () {
				to_url && to_url();
			}, endtime);
		} else {
			//没有提示时跳转不延迟
			setTimeout(function () {
				uni.navigateTo({
					url: to_url,
				})
			}, title ? endtime : 0);
		}
	}
}

export const uploadFile = (file : string) => {
	return new Promise((reslove, reject) => {
		let date : any = new Date();
		let y = date.getFullYear()
		let m = date.getMonth() + 1
		m = m < 10 ? ('0' + m) : m
		let d = date.getDate()
		d = d < 10 ? ('0' + d) : d
		let dateTime = 'storage/default/' + y + '' + m + '' + d + '/';

		ossUpload(file, file, dateTime).then((res : any) => {
			console.log(res)
			reslove(res.data);
		}).catch((err : any) => {
			reject(err || '上传失败');
		})
		// uni.uploadFile({
		// 	url: config.HTTP_REQUEST_URL + '/api/ajax/upload',
		// 	filePath: file,
		// 	name: 'file',
		// 	header: {
		// 		Accept: 'application/json, text/plain, */*',
		// 		'Accept-Language': 'zh-CN,zh;q=0.9',
		// 		server: true,
		// 		'ba-user-token': uni.getStorageSync('token')
		// 	},
		// 	formData: {},
		// 	success: (res : any) => {
		// 		console.log(res)
		// 		let data = JSON.parse(res.data)
		// 		reslove(data.data.file.full_url);
		// 	},
		// 	fail: (err) => {
		// 		reject(err);
		// 	}
		// })
	})
}

export const getToday = () : String => {
	let d = new Date()
	return d.getFullYear() + '-' + (((d.getMonth() + 1) + '').padStart(2, '0')) + '-' + ((d.getDate() + '').padStart(2, '0'))
}

export const getCurMonth = () : String => {
	let d = new Date()
	return d.getFullYear() + '-' + (((d.getMonth() + 1) + '').padStart(2, '0'))
}

export const addMonths = (months : number) => {
	let date = new Date()
	// 创建一个新的Date对象，传入原始日期的年份和月份
	var newDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());

	// 获取原始日期的月份
	var originalMonth = newDate.getMonth();

	// 将月份增加3
	newDate.setMonth(newDate.getMonth() + months);

	// 如果新的月份超过12，将年份加1，月份减12
	if (newDate.getMonth() < originalMonth) {
		newDate.setFullYear(newDate.getFullYear() + 1);
		newDate.setMonth(newDate.getMonth() - 12);
	}

	return newDate.getFullYear() + '-' + ((newDate.getMonth() + 1) + '').padStart(2, '0') + '-' + ((newDate.getDate() + '').padStart(2, '0'));
}

export const getUserInfo = () => {
	return request.get('house/user/userInfo')
}

export const noVipShow = () => {
	uni.showModal({
		title: '温馨提示',
		content: '分享5位摊友即可赠送会员，享受高级功能，请进入首页后进行分享，是否立即分享？',
		success: (res) => {
			if (res.confirm) {
				gotoPage('/pages/index/index')
			}
		}
	})
}